import java.time.LocalDate

object Sheet2 {
  def main(args: Array[String]): Unit = {
    val result1 = product1("Hello")
    println(result1)

    val result2 = product2("Hello")
    println(result2)

    val result3 = compute(5, 2)
    println(result3)

    val year = 2011
    val month = 11
    val day = 11
    println(date"$year-$month-$day")
  }

  def product1(s: String): Long = {

    def productor(index: Int, product: Long): Long = {
      if (index == s.length) {
        product
      } else {
        productor(index + 1, product * s(index))
      }
    }

    productor(0, 1)
  }

  def product2(s: String): Long = {
    if (s.length == 1)
      s.charAt(0).toLong
    else
      s.charAt(0) * product2(s.drop(1))
  }

  def compute(x: Long, n: Long): Long = {
    if (n == 0) {
      1
    } else if (n > 0) {
      if (n % 2 == 0) {
        // even
        compute(x, n / 2) * compute(x, n / 2)
      } else {
        // odd
        x * compute(x, n - 1)
      }
    } else {
      // n < 0
      1 / compute(x, -n)
    }
  }

  implicit class DateInterpolator(val sc: StringContext) extends AnyVal {
    def date(args: Any*): LocalDate = {
      if (args.length != 3) {
        throw new IllegalArgumentException("args.length != 3")
      }

      LocalDate.of(args(0).asInstanceOf[Int], args(1).asInstanceOf[Int], args(2).asInstanceOf[Int])
    }
  }

}
